<?php

class Manga_manage extends CI_Controller {

    var $controller_url;

    public function __construct() {
        parent::__construct();

        //Set folder to load view (under View folder)
        $this->layouts->set_view_folder('manga');

        //Load neccessary JS and CSS
        $this->layouts->set_js('jquery.dataTables');

        $this->layouts->set_css('datatable/demo_table');
        
        //$this->layouts->set_layout('blank');

        //Load models
        $this->load->model('admin/manga_manage_model');

        //Load libraries
        $this->load->library(array('table', 'form_validation'));

        //Load helpers
        $this->load->helper(array('form', 'file', 'array'));

        //Set controller url to be used later
        $this->controller_url = 'admin/manga_manage/';

        //$this->output->enable_profiler(TRUE);
    }

    function index() {

        //If user come back to index after editing a manga, delete manga_id session and upload_errors session
        if ($this->session->userdata('manga_id') !== FALSE) {
            $this->session->unset_userdata('manga_id');
        }

        if ($this->session->userdata('uploading_errors') !== FALSE) {
            $this->session->unset_userdata('uploading_errors');
        }

        $data['all_manga'] = $this->manga_manage_model->get_all_manga();
        $data['controller_url'] = $this->controller_url;

        $this->layouts->view('manga_manage_list', $data);
    }

    function add_new_manga() {

        //Receive data from POST and validate
        $this->form_validation->set_rules('manga_title', 'manga title', 'trim|required|xss_clean|callback_title_exists');

        if ($this->form_validation->run() == FALSE) {
            //Validation has not run or there are errors
            $this->index();
        } else {
            //Validated --> Add title to database
            //Get data from POST
            $title = $this->input->post('manga_title');

            //Add and redirect to manage manga view
            $this->manga_manage_model->add_manga($title);
            $this->index();
        }
    }

    function title_exists($title) {
        $this->form_validation->set_message('title_exists', 'This %s already exists. Please choose another title and try again');

        if ($this->manga_manage_model->check_title_exists($title)) {
            //If TRUE, title exists --> cannot proceed
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function delete_manga($manga_id = NULL) {
        if ($manga_id != NULL) {
            if ($this->manga_manage_model->delete_manga($manga_id) == TRUE) {
                redirect($this->controller_url);
            } else {
                echo "Error";
            }
        } else {
            redirect($this->controller_url);
        }
    }

    function edit_manga($manga_id = NULL) {
        if ($manga_id === NULL) {
            redirect($this->controller_url);
        } else {
            //First time access edit manga page, get manga_id from URI and store into session
            if ($this->session->userdata('manga_id') === FALSE) {
                $manga_id = $this->uri->segment(4);
                $this->session->set_userdata('manga_id', $manga_id);
            } else {
                $manga_id = $this->session->userdata('manga_id');
            }

            //Get manga details and pass to $data array
            $data['manga_title'] = $this->manga_manage_model->get_title($manga_id);
            $data['all_pages'] = $this->manga_manage_model->get_all_pages($manga_id);
            $data['last_page'] = $this->manga_manage_model->get_last_page($manga_id);
            $data['all_characters'] = $this->manga_manage_model->get_all_characters($manga_id);
            $data['avail_personality'] = $this->manga_manage_model->get_personality($manga_id);
            $data['manga_id'] = $manga_id;
            $data['controller_url'] = $this->controller_url;

            //Get uploading errors (if have)
            $data['uploading_errors'] = $this->session->userdata('uploading_errors');

            //Load JS and CSS: Fancybox to display image
            $this->layouts->set_js('jquery.fancybox-1.3.4');
            $this->layouts->set_css('fancybox/jquery.fancybox-1.3.4');

            //Output to view
            $this->layouts->view('edit_manga', $data);
        }
    }

    function upload_page() {

        if ($this->input->post('submit')) {
            //Upload and redirect
            //Need manga id and page number
            $manga_id = $this->session->userdata('manga_id');
            $page = $this->input->post('pages'); //Get from drop down value            

            $uploading_errors = $this->manga_manage_model->upload_page($manga_id, $page);

            if (empty($uploading_errors)) {
                $this->session->unset_userdata('uploading_errors');
            } else {
                $this->session->set_userdata('uploading_errors', $uploading_errors);
            }

            redirect($this->controller_url . 'edit_manga/' . $manga_id);
        } else {
            redirect($this->controller_url);
        }
    }

    function delete_page($page_id = NULL) {

        $manga_id = $this->session->userdata('manga_id');

        if ($page_id != NULL) {
            if ($this->manga_manage_model->delete_page($manga_id, $page_id) === TRUE) {
                redirect($this->controller_url . 'edit_manga/' . $manga_id);
            } else {
                echo "Error";
                //Do redirect
            }
        } else {
            //Do redirect
            redirect($this->controller_url . 'edit_manga/' . $manga_id);
        }
    }

    function upload_char() {
        if ($this->input->post('submit')) {

            //Upload and redirect
            //Need manga id and personality type
            $manga_id = $this->session->userdata('manga_id');
            $personality = $this->input->post('personality'); //Get from drop down value

            $uploading_errors = $this->manga_manage_model->upload_char($manga_id, $personality);

            if (empty($uploading_errors)) {
                $this->session->unset_userdata('uploading_errors');
            } else {
                $this->session->set_userdata('uploading_errors', $uploading_errors);
            }
            redirect($this->controller_url . 'edit_manga/' . $manga_id);
        } else {
            redirect($this->controller_url);
        }
    }

    function delete_char($char_id = NULL) {

        $manga_id = $this->session->userdata('manga_id');

        if ($char_id != NULL) {
            if ($this->manga_manage_model->delete_char($char_id) === TRUE) {
                redirect($this->controller_url . 'edit_manga/' . $manga_id);
            } else {
                echo "Error";
                //Do redirect
            }
        } else {
            //Do redirect
            redirect($this->controller_url . 'edit_manga/' . $manga_id);
        }
    }

}

/* End of file manga_manage.php */
/* Location: ./application/controllers/manga_manage.php */

